package com.yanqu.road.server.manger.config;

import com.yanqu.road.dao.impl.activity.hotpotjump.CommonLocalConfigDaoImpl;
import com.yanqu.road.entity.autodata.task.OldTaskChangeConfigData;
import com.yanqu.road.entity.enums.eTaskType;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.TaskInfo;
import com.yanqu.road.server.TempMgr;

import java.util.*;

public class OldTaskChangeMgr extends TempMgr {
    private static Map<Integer, OldTaskChangeConfigData> oldTaskChangeMap = new HashMap<>();

    /**
     * 需要转换的旧任务类型
     */
    private static Set<Integer> needTransformTaskType = new HashSet<>();

    static {
        needTransformTaskType.add(eTaskType.Cricket.getValue());
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        loadConfigDb();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void loadConfigDb() {
        List<OldTaskChangeConfigData> tempList = new CommonLocalConfigDaoImpl<OldTaskChangeConfigData>().getConfigList(OldTaskChangeConfigData.class);
        Map<Integer, OldTaskChangeConfigData> tempMap = new HashMap<>();
        for (OldTaskChangeConfigData temp : tempList) {
            tempMap.put(temp.getId(), temp);
        }
        oldTaskChangeMap = tempMap;

        checkConfig();
    }

    public static Map<Integer, OldTaskChangeConfigData> getOldTaskChangeMap() {
        return oldTaskChangeMap;
    }

    public static OldTaskChangeConfigData getOldTaskChangeConfig(int oldTaskId) {
        return oldTaskChangeMap.get(oldTaskId);
    }

    public static boolean isNeedTransformTask(Integer taskType) {
        return needTransformTaskType.contains(taskType);
    }

    /**
     * 检查配置是否正确
     */
    public static void checkConfig() {
        for (OldTaskChangeConfigData temp : oldTaskChangeMap.values()) {
            TaskInfo taskInfoById = TaskMgr.getTaskInfoById(temp.getNewId());
            if (taskInfoById == null) {
                throw new RuntimeException("旧任务转新任务配置错误，新任务" + temp.getNewId() + "不存在，旧任务ID为：" + temp.getId());
            }
            TaskConditionInfo taskConditionById = TaskMgr.getTaskConditionById(temp.getNewId());
            if (taskConditionById == null) {
                throw new RuntimeException("旧任务转新任务condition配置错误，新任务" + temp.getNewId() + "不存在，旧任务ID为：" + temp.getId());
            }
        }
    }
}
